package cn.itcast.bos.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.PromotionRepository;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.PromotionService;

@Service
@Transactional
public class PromotionServiceImpl implements PromotionService {
	
	@Autowired
	private PromotionRepository promotionRepository;
	
	@Override
	public void save(Promotion model) {
		promotionRepository.save(model);
	}

	@Override
	public Page<Promotion> pageQuery(Pageable pageable) {
		Page<Promotion> page = promotionRepository.findAll(pageable);
		return page;
	}

	@Override
	public PageBean<Promotion> findPageData(int page, int rows) {
		System.out.println("进入了findPageData服务方法");
		//将page和rows进行封装
		Pageable pageable = new PageRequest(page - 1, rows); //这里的-1不能忘记,是从索引0开始的
		Page<Promotion> page2 = promotionRepository.findAll(pageable);
		//封装一个PageBean对象作为返回值
		PageBean<Promotion> pageBean = new PageBean<Promotion>();
		pageBean.setTotalCount(page2.getTotalElements());
		pageBean.setPageData(page2.getContent());
		
		System.out.println("==>"+pageBean.getPageData());
		
		return pageBean;
	}
	
	@Override
	public Promotion findById(Integer id) {
		return promotionRepository.findOne(id);
	}
	
	//定时查询宣传活动是否过期
	@Override
	public void updateStatus(Date date) {
		promotionRepository.updateStatus(date);
	}

}
